package com.windward.vii.dao.mapper;

import com.windward.vii.dao.entity.UserToken;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface UserTokenMapper {
    @Delete({
        "delete from user_token",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into user_token (id, uid, token, ",
        "expire_time, terminal_type)",
        "values (#{id,jdbcType=BIGINT}, #{uid,jdbcType=BIGINT}, #{token,jdbcType=VARCHAR}, ",
        "#{expireTime,jdbcType=TIMESTAMP}, #{terminalType,jdbcType=TINYINT})"
    })
    int insert(UserToken record);

    int insertSelective(UserToken record);

    @Select({
        "select",
        "id, uid, token, expire_time, terminal_type",
        "from user_token",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @ResultMap("BaseResultMap")
    UserToken selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(UserToken record);

    @Update({
        "update user_token",
        "set uid = #{uid,jdbcType=BIGINT},",
          "token = #{token,jdbcType=VARCHAR},",
          "expire_time = #{expireTime,jdbcType=TIMESTAMP},",
          "terminal_type = #{terminalType,jdbcType=TINYINT}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(UserToken record);

    /***********以下为自定义方法************************/
    @Select({
            "select",
            "id, uid, token, expire_time",
            "from user_token",
            "where token = #{token,jdbcType=VARCHAR} and expire_time > CURRENT_TIMESTAMP"
    })
    @ResultMap("BaseResultMap")
    UserToken getByToken(String token);

    @Delete({
            "delete from user_token",
            "where token = #{token,jdbcType=VARCHAR}"
    })
    int deleteByToken(String token);

    @Delete({
            "delete from user_token ",
            "where uid = #{uid,jdbcType=BIGINT} and terminal_type = #{terminalType,jdbcType=TINYINT}"
    })
    int deleteByUidAndTerminalType(Map<String, Object> map);

    int updateByTokenSelective(UserToken record);

    @Select({
            "select",
            "id, uid, token, expire_time",
            "from user_token",
            "where uid = #{uid,jdbcType=BIGINT} and expire_time > CURRENT_TIMESTAMP"
    })
    @ResultMap("BaseResultMap")
    List<UserToken> getTokenByUid(Long uid);
}